//SKU主数据主表

$(function(){

    gridWidth = $('.u_toolbar');

    _gridInit();

    $("li[data-opt='edit']").on('click', function(){
        var rowId = _grid.jqGrid('getGridParam', 'selrow');
        editCode(rowId);
    });
    $("li[data-opt='opt_search']").find(">i").on('click', function(){
        gridSearch.Content = {
            SearchText: $("#opt_search").find(">input").val()
        };
        search();
    });

});

var _grid = $('#jgTable'); 

function detailCode(rowId){
    if(rowId){  //判断是否选中有效行
        var rowData = _grid.jqGrid('getRowData', rowId);
        window.open('MDM000403.html?Code=' + rowData.Code, '_blank');
    }else{
        myAlert('请选择您要编辑的数据');
    }
}

function editCode(rowId){
    if(rowId){  //判断是否选中有效行
        var rowData = _grid.jqGrid('getRowData', rowId);
        window.open('MDM000402.html?Code=' + rowData.Code, '_blank');
    }else{
        myAlert('请选择您要编辑的数据');
    }   
}

function alarmFormatter(cellvalue, options, rowdata){
    var grid = $(this);
    if(JSON.stringify(rowdata)!=='{}'){
        cellvalue = '<a href="javascript:0" opt-data="'+grid[0].id+'_detail" title="编辑详情页"><i class="fa fa-pencil"></i></a>'
        +'<a href="javascript:0" opt-data="'+grid[0].id+'_edit" title="查看详情"><i class="fa fa-eye"></i></a>';
    }else{
        cellvalue = '';
    }
    return cellvalue;
}
function _gridInit(){
    var _gridData = Object.create(gridData);
    _gridData.url = mdmPath + '/MDM0004/MDM000402';
    _gridData.colModel = [
        {label:'代码', name:'Code', width:60},
        {label:'名称', name:'Name'},
        {label:'品牌', name:'Brand', width:60},
        {label:'操作', name:'Options', width:60, formatter: alarmFormatter, sortable:false}
    ];
    _gridData.sortname = 'Name';
    var _postData = {
        Method: 'Q',
        Content:{
            SearchText: ''
        }
    };
    for(i in postData){
        _postData[i] = postData[i];
    }
    //console.log(postData)
    _gridData.beforeRequest = function(){
        _gridData.grid.jqGrid("setGridParam", {
            postData : _postData,
        });
    };
    _gridData.loadComplete = function(xhr){
        xhr.Status == -999 && alert_QX($('body'), xhr.Message);
        search();
        $('a[opt-data="jgTable_detail"]').unbind('click').bind('click', function(){
            var rowId = $(this).parent().parent()[0].id;
            detailCode(rowId);
        });
        $('a[opt-data="jgTable_edit"]').unbind('click').bind('click', function(){
            var rowId = $(this).parent().parent()[0].id;
            editCode(rowId);
        });
        $('a[opt-data="jgTable_delete"]').unbind('click').bind('click', function(){
            var rowId = $(this).parent().parent()[0].id;
            deleteCode(rowId);
        });
    };
    _gridData.ondblClickRow = function(rowid,iRow,iCol,e){
        editCode(rowid);
    };
    gridInit(_gridData);
    gridSearch = _postData;
}